package com.supply.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supply.global.Constants;
import com.supply.pojo.*;
import com.supply.service.*;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private TaskProcessService taskProcessService;

    @Autowired
    private LeaveProcessService leaveProcessService;
    @Autowired
    private OutBoundProcessService outBoundProcessService;
    @Autowired
    private ProcurementProcessService procurementProcessService;
    @Autowired
    private ProcurementBillService procurementBillService;
    @Autowired
    private LeaveBillService leaveBillService;
    @Autowired
    private OutBoundBillService outBoundBillService;

    //根据用户ID查询task任务信息
    @GetMapping("/queryByUserId/{id}")
    public ResultBean queryByUserId(@PathVariable("id") Long id, PageData pageData) {
        System.out.println("要查找待办事务的条件：" + id);
        PageHelper.startPage(pageData.getPageNum(), pageData.getPageSize());
        List<Task> tasks = taskProcessService.queryByUserId(id);
        PageInfo pageInfo = new PageInfo(tasks);
        //task数据比较多，所以这里只提取需要的数据封装返回
        List<Map> rs = new ArrayList<>();
        for (Task task : tasks) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", task.getId());
            map.put("name", task.getName());
            map.put("createTime", task.getCreateTime());
            rs.add(map);
        }
        pageInfo.setList(rs);
        return ResultBean.success(pageInfo);
    }

    //根据taskId获取task任务相关的表单信息，包括当前任务可执行的命令、关联的申请单数据以及关联的批注列表
    @GetMapping("/viewTaskForm/{taskId}")
    public ResultBean viewTaskForm(@PathVariable("taskId") String taskId) {
        System.out.println("请求的taskID：" + taskId);
        //获取唯一标识businessKey，从而判断当前任务属于哪个申请流程
        String businessKey = taskProcessService.getBusinessKeyByTaskId(taskId);
        String applyType = businessKey.split("\\.")[0];
        Map<String, Object> map = new HashMap<>();

        //获取对应申请单信息
        if (applyType.equals(Constants.LEAVE_KEY)) {
            LeaveBill leaveBill = leaveProcessService.queryLeaveBillByTaskId(taskId);
            map.put("leaveBill", leaveBill);
        } else if (applyType.equals(Constants.PROCUREMENT_KEY)) {
            ProcurementBill procurementBill = procurementProcessService.queryProcurementBillByTaskId(taskId);
            map.put("procurementBill", procurementBill);
        } else if (applyType.equals(Constants.OUTBOUND_KEY)) {
            OutBoundBill outBoundBill = outBoundProcessService.queryOutBoundBillByTaskId(taskId);
            map.put("outBoundBill", outBoundBill);
        }

        //获取关联的批注信息列表
        List<Comment> comments = taskProcessService.queryAllCommentByTaskId(taskId);
        //获取当前任务可执行的命令
        List<String> buttonList = taskProcessService.queryButtonListByTaskId(taskId);

        map.put("comments", comments);
        map.put("buttonList", buttonList);
        map.put("applyType", applyType);

        return ResultBean.success(map);
    }

    //流程推进
    @PostMapping("/complete")
    public ResultBean complete(@RequestBody TaskCompleteData taskCompleteData) {
        System.out.println("推进流程的数据：" + taskCompleteData);
        taskProcessService.complete(taskCompleteData);

        return ResultBean.success();
    }

    //查询审核中的审核记录信息
    @GetMapping("/currentAuditTrail/{applyType}/{id}")
    public ResultBean currentAuditTrail(@PathVariable("applyType") String applyType,
                                        @PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();

        if (applyType.equals(Constants.LEAVE_KEY)) {
            LeaveBill leaveBill = leaveBillService.queryById(id);
            map.put("leaveBill", leaveBill);
        } else if (applyType.equals(Constants.OUTBOUND_KEY)) {
            OutBoundBill outBoundBill = outBoundBillService.queryById(id);
            map.put("outBoundBill", outBoundBill);
        } else if (applyType.equals(Constants.PROCUREMENT_KEY)) {
            ProcurementBill procurementBill = procurementBillService.queryById(id);
            map.put("procurementBill", procurementBill);
        }

        String businessKey = applyType + "." + id;
        //根据businessKey查询审核中的流程的批注信息
        List<Comment> comments = taskProcessService.getCurrentCommentsByBusinessKey(businessKey);
        map.put("comments", comments);
        return ResultBean.success(map);
    }

    @GetMapping("/overAuditTrail/{applyType}/{id}")
    public ResultBean overAuditTrail(@PathVariable("applyType") String applyType,
                                     @PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();

        if (applyType.equals(Constants.LEAVE_KEY)) {
            LeaveBill leaveBill = leaveBillService.queryById(id);
            map.put("leaveBill", leaveBill);
        } else if (applyType.equals(Constants.OUTBOUND_KEY)) {
            OutBoundBill outBoundBill = outBoundBillService.queryById(id);
            map.put("outBoundBill", outBoundBill);
        } else if (applyType.equals(Constants.PROCUREMENT_KEY)) {
            ProcurementBill procurementBill = procurementBillService.queryById(id);
            map.put("procurementBill", procurementBill);
        }

        String businessKey = applyType + "." + id;
        //根据businessKey查询审核结束的流程的批注信息
        List<Comment> comments = taskProcessService.getOverCommentsByBusinessKey(businessKey);
        map.put("comments", comments);
        return ResultBean.success(map);
    }

    //删除流程实例
    @DeleteMapping("/delete/{taskId}")
    public ResultBean delete(@PathVariable("taskId") String taskId) {
        int rs = taskProcessService.deleteByTaskId(taskId);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }

    //获取当前任务的处理进度
    @GetMapping("/getCurrentProgress/{applyType}/{id}")
    public ResultBean getCurrentProgress(@PathVariable("applyType") String applyType,
                                         @PathVariable("id") Long id) {

        String progressName = taskProcessService.getCurrentProgress(applyType + "." + id);
        return ResultBean.success(progressName);
    }
}
